package org.yoao.crm.tree;

import java.util.ArrayList;
import java.util.List;

import org.yoao.crm.struts.form.MenuForm;

public class StringUtil {
	private final String bigSeparator = ",";
	private final String smallSeparator = "#";
	
	public String[] bigSplit(String str) {
		String[] result = null; 
		if(str != null && !str.trim().equals("")) {
			result = str.split(bigSeparator);
		}
		return result;
	}	
	
	/**
	 * 0#0#说明#StaticForm#0#1,
	 * 1#0#类别测试#StaticForm#1#1,
	 * 2#0#报表#MarkForm#2#1,
	 * 3#1#test#ReportForm#3#1,
	 * 4#1#复制：test#ReportForm#4#1,
	 * 5#1#测试报表#ReportForm#5#1,
	 * 6#1#22222333#ReportForm#6#1 
	 * @param str
	 * @param form
	 * @return
	 */
	
	public MenuForm smallSplit(String str) {
		String[] result = null; 
		MenuForm menuItem = null;
		if(str != null && !str.trim().equals("")) {
			result = str.split(smallSeparator);
			if(result != null) {
				menuItem = new MenuForm();
				try{
					menuItem.setSortNumber(result[0].equals("")?0:Integer.parseInt(result[0]));
					menuItem.setLayer(result[1].equals("")?0:Integer.parseInt(result[1]));
					menuItem.setTitle(result[2]);
					menuItem.setFormType(result[3]);
					menuItem.setKeyName(result[4]);
					menuItem.setIsActive(result[5].equals("")?0:Integer.parseInt(result[5]));					
				} catch(Exception e) {
					menuItem = null;
					e.printStackTrace();
				}
			}
		}
		return menuItem;
	}
	
	public List split(String viewTurn) {
		String[] strArray = null;
		List result = null;
		MenuForm tmp = null;
		
		strArray = bigSplit(viewTurn);
		if(strArray != null) {
			result = new ArrayList();
			for(int i=0;i<strArray.length;i++) {
				tmp = smallSplit(strArray[i]);
				if(tmp != null) {
					result.add(tmp);
				} else {
					result = null;
					break;
				}
			}
		}
		return result;
	}
}
